"""
FileName：product_page.py
Description：
Author：gaojiahui
Time：2025/8/29 18:38
Website：https://gitee.com/gao-jiahui
"""
from selenium.webdriver.common.by import By

from pages.base_page import BasePage


class ProductPage(BasePage):
    """商品详情页"""
    # 元素定位
    PRODUCT_TITLE = (By.TAG_NAME, "h1")
    ADD_TO_CART_BUTTON = (By.CSS_SELECTOR, "button.buy-btn.buy")
    QUANTITY_INPUT = (By.CSS_SELECTOR, "input[type='number']")
    PRICE = (By.CSS_SELECTOR, "div.pro-content.price div.price-text")
    CONFIRM_BUTTON = (By.CSS_SELECTOR, "button.confirm-btn")

    def get_product_title(self):
        """获取商品标题"""
        return self.get_text(self.PRODUCT_TITLE)

    def get_product_price(self):
        """获取商品价格"""
        return self.get_text(self.PRICE)

    def set_quantity(self, quantity):
        """设置商品数量"""
        self.send_keys(self.QUANTITY_INPUT, str(quantity))

    def add_to_cart(self):
        """添加商品到购物车"""
        self.click(self.ADD_TO_CART_BUTTON)
        self.click(self.CONFIRM_BUTTON)
        # 点击确认按钮后会跳转到购物车页面
        from pages.cart_page import CartPage
        return CartPage(self.driver)
